import CryptoJS from 'crypto-js';

/**
 * 随机生成32位的字符串
 * @returns {string}
 */
export function generateRandomString(){
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';
  const charactersLength = characters.length
  for (let i = 0; i < 32; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result
}

/**
 * 随机生成aes 密钥
 * @returns {string}
 */
export function generateAesKey(){
  return CryptoJS.enc.Utf8.parse(generateRandomString());
}

/**
 * 加密base64
 * @returns {string}
 */
export function encryptBase64(str){
  return CryptoJS.enc.Base64.stringify(str);
}

/**
 * 使用密钥对数据进行加密
 * @param message
 * @param aesKey
 * @returns {string}
 */
export function encryptWithAes(message, aesKey){
  const encrypted = CryptoJS.AES.encrypt(message, aesKey, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });
  return encrypted.toString();
}

/**
 * 使用密钥对数据进行解密
 * @param encrypted
 * @param aesKey
 * @returns {string}
 */
export function decryptWithAes(encrypted, aesKey) {
  try {
    const decrypted = CryptoJS.AES.decrypt(encrypted, aesKey, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return decrypted.toString(CryptoJS.enc.Utf8);
  } catch (e) {
    console.warn('decryptWithAes failed', e);
    return null;
  }
}

/**
 * base64 字符串解码为 CryptoJS WordArray（用于还原 AES key）
 * @param base64Str
 * @returns {CryptoJS.lib.WordArray}
 */
export function decryptBase64(base64Str){
  try {
    return CryptoJS.enc.Base64.parse(base64Str);
  } catch (e) {
    console.warn('decryptBase64 failed', e);
    return null;
  }
}
